Input/Output Stream
-
C++ uses a convenient abstraction called streams to perform input and output operations in sequential media such as the screen, the keyboard or a file. A stream is an entity where a program can either insert or extract characters to/from. There is no need to know details about the media associated to the stream or any of its internal specifications. All we need to know is that streams are a source/destination of characters, and that these characters are provided/accepted sequentially
-
The standard library defines a handful of stream objects that can be used to access what are considered the standard sources and destinations of characters by the environment where the program runs:
-
std::cin-
standard input stream
-
means "character in".
-
-
std::cout-
standard output stream
-
means "character out".
-
-
std::cerr-
standard error (output) stream
-
-
std::clog-
standard logging (output) stream
-
-
-
std::cerrandstd::clogare also output streams, so they essentially work like cout, with the only difference being that they identify streams for specific purposes: error messages and logging; which, in many cases, in most environment setups, they actually do the exact same thing: they print on screen, although they can also be individually redirected. -
<<-
It's called "insertion operator". It inserts the data that follows it into the stream that precedes it.
-
Multiple insertion operations (<<) may be chained in a single statement:
cout << "This " << " is a " << "single C++ statement"; -
-
std::endl-
It does exactly as the insertion of '\n' does; but it also has an additional behavior: the stream's buffer (if any) is flushed, which means that the output is requested to be physically written to the device, if it wasn't already.
-
It is generally a good idea to use endl only when flushing the stream would be a feature and '\n' when it would not. Bear in mind that a flushing operation incurs a certain overhead, and on some devices it may produce a delay.
-
-
>>-
This operator is then followed by the variable where the extracted data is stored.
-
This operation makes the program wait for input from cin; generally, this means that the program will wait for the user to enter some sequence with the keyboard. In this case, note that the characters introduced using the keyboard are only transmitted to the program when the ENTER (or RETURN) key is pressed. Once the statement with the extraction operation on cin is reached, the program will wait for as long as needed until some input is introduced.
-
The extraction operation on cin uses the type of the variable after the >> operator to determine how it interprets the characters read from the input; if it is an integer, the format expected is a series of digits, if a string a sequence of characters, etc.
-
What happens if the user enters something else that cannot be interpreted as the type of the variable? Well, in this case, the extraction operation fails. And this, by default, lets the program continue without setting a value for variable i, producing undetermined results if the value of i is used later.
-
-
Extractions on cin can also be chained to request more than one datum in a single statement:
cin >> a >> b;-
This is equivalent to:
cin >> a; cin >> b; -
-
#include <iostream>
#include <string>
int main ()
{
std::string my_string;
my_string = "This is the initial string content";
std::cout << my_string << std::endl;
my_string = "This is a different string content";
std::cout << my_string << std::endl;
return 0;
}
Stringstream
-
The standard header
<sstream>defines a type called stringstream that allows a string to be treated as a stream, and thus allowing extraction or insertion operations from/to strings in the same way as they are performed on cin and cout. This feature is most useful to convert strings to numerical values and vice versa. For example, in order to extract an integer from a string we can write:
string my_str ("1204");
int my_int;
stringstream(mys_tr) >> my_int;
Reading/Writing to Files
-
ofstream:-
Stream class to write on files
-
-
ifstream:-
Stream class to read from files
-
-
fstream:-
Stream class to both read and write from/to files.
-
#include <iostream>
#include <fstream>
int main () {
std::ofstream my_file;
my_file.open("example.txt");
my_file << "Writing this to a file.\n";
my_file.close();
return 0;
}